package com.xintonghua.bussiness.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.SimpleBaseAdapter;
import com.xintonghua.bussiness.util.ImageTools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddPhotosAdapter extends SimpleBaseAdapter<String> {
    private int number = 0;
    private OnClickAddListener onClickAddListener;
    private boolean isAdd = true;
    private boolean isDelete = false;

    public AddPhotosAdapter(Context context, int number, OnClickAddListener onClickAddListener) {
        super(new ArrayList<String>(), context);
        this.number = number;
        this.onClickAddListener = onClickAddListener;
        showAdd();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_grid_photo, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final String url = mList.get(position);

        if (TextUtils.isEmpty(url) && isAdd) {
            holder.imgDelete.setVisibility(View.GONE);
            ImageTools.displayImage(url, holder.imgPhoto, R.drawable.ic_sj_add_photos);
        } else {
            holder.imgDelete.setVisibility(View.VISIBLE);
            ImageTools.displayImage(url, holder.imgPhoto);

        }

        autoHideShowAdd();

        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete(position);
            }
        });

        holder.imgPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(url)) {
                    onClickAddListener.onClickAdd(position);
                }
            }
        });

        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.img_photo)
        ImageView imgPhoto;
        @BindView(R.id.img_delete)
        ImageView imgDelete;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 点击删除
     *
     * @param position
     */
    private void delete(int position) {
        mList.remove(position);
        if (mList.size() == number - 1 && !isDelete) {
            isDelete = true;
            mList.add("");
            isAdd = true;
        }
        notifyDataSetChanged();
    }


    /**
     * 获取图片张数
     *
     * @return
     */
    public int getPhotoCount() {
        return isAdd == true ? getCount() - 1 : getCount();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addAllData(List<String> list) {
        if (isAdd) {
            HideAdd();
        }
        mList = list;
        if (mList.size() == number) {
            isDelete = false;
        }
        showAdd();
        notifyDataSetChanged();
    }


    /**
     * 显示add按钮
     */
    public void showAdd() {
        if (mList.size() < number) {
            mList.add("");
            isAdd = true;
            notifyDataSetChanged();
        }
    }

    /**
     * 移除add按钮
     */
    public void HideAdd() {
        int lastPosition = mList.size() - 1;
        if (TextUtils.isEmpty(mList.get(lastPosition))) {
            mList.remove(lastPosition);
            isAdd = false;
            notifyDataSetChanged();
        }
    }


    /**
     * 移除add按钮
     */
    public void autoHideShowAdd() {
        int lastPosition = mList.size() - 1;
        if (lastPosition == number && TextUtils.isEmpty(mList.get(lastPosition))) {
            mList.remove(lastPosition);
            isAdd = false;
            isDelete = false;
            notifyDataSetChanged();
        } else if (!isAdd) {
            showAdd();
        }
    }

    /**
     * 获取数据
     *
     * @return
     */
    public List<String> getDataList() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            if (!TextUtils.isEmpty(mList.get(i))) {
                list.add(mList.get(i));
            }
        }
        return list;
    }


    /**
     * 加号接口
     */
    public interface OnClickAddListener {
        void onClickAdd(int position);
    }


}
